package edu.sysu.cshare.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.Session;

import edu.sysu.cshare.util.HibernateSessionFactory;

@Entity
@Table(name = "T_COURSE")
public class Course implements Serializable {
	public static Course getById(long id) {
		Session ss = HibernateSessionFactory.getSession();
		return (Course) ss.load(Course.class, id);
	}

	private List<Clazz> classes = new ArrayList<Clazz>();

	private String examDesc;

	private List<Group> groups = new ArrayList<Group>();

	private List<Homework> homeworks = new ArrayList<Homework>();

	private long id;

	private short maxCntPerGroup;
	private String name;
	private int points;
	private Set<User> students = new HashSet<User>();
	private User teacher;
	private String type;
	public Course() {

	}

	/**
	 * 
	 * @param name
	 *            课程名称
	 * @param type
	 *            课程类型
	 * @param points
	 *            课程学分
	 * @param examDesc
	 *            考试说明
	 */
	public Course(String name, List<Clazz> classes,String type, int points, String examDesc) {
		super();
		this.examDesc = examDesc;
		this.classes=classes;
		this.name = name;
		this.points = points;
		this.type = type;
	}

	@ManyToMany(targetEntity = Clazz.class)
	@JoinTable(name = "TR_COURSE_CLASS", joinColumns = @JoinColumn(name = "COURSE_ID", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "ClAZZ_ID", referencedColumnName = "id"))
	public List<Clazz> getClasses() {
		return classes;
	}

	public String getExamDesc() {
		return examDesc;
	}

	@OneToMany(mappedBy = "course" ,cascade=CascadeType.REMOVE)
	public List<Group> getGroups() {
		return groups;
	}

	public boolean hasStudent(User stu){
		return students.contains(stu);
	}
	@OneToMany(mappedBy = "course",cascade=CascadeType.REMOVE)
	public List<Homework> getHomeworks() {
		return homeworks;
	}

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public short getMaxCntPerGroup() {
		return maxCntPerGroup;
	}

	public String getName() {
		return name;
	}

	public int getPoints() {
		return points;
	}

	@ManyToMany(mappedBy = "courses",targetEntity=User.class)
	public Set<User> getStudents() {
		return students;
	}

	@OneToOne
	public User getTeacher() {
		return teacher;
	}

	public String getType() {
		return type;
	}
	
	public void setClasses(List<Clazz> classes) {
		this.classes = classes;
	}

	public void setExamDesc(String examDesc) {
		this.examDesc = examDesc;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public void setHomeworks(List<Homework> homeworks) {
		this.homeworks = homeworks;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setMaxCntPerGroup(short maxCntPerGroup) {
		this.maxCntPerGroup = maxCntPerGroup;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public void setStudents(Set<User> students) {
		this.students = students;
	}

	public void setTeacher(User teacher) {
		this.teacher = teacher;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "课程:"+name;
	}

}
